<template>
  <div class="element_manager">
    <div class="element_title">{{ id ? '编辑' : '新增'}}元素</div>
    <el-form ref="formRef" :model="form" :rules="rules">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="form.name"
          maxlength="50"
          show-word-limit
        ></el-input>
      </el-form-item>
      <el-form-item label="化学表达式" prop="name_en">
        <el-input
          v-model="form.chemical_tag"
          maxlength="50"
          show-word-limit
        ></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="main_text">
        <el-input
          v-model="form.main_text"
          type="textarea"
          :rows="15"
          maxlength="2000"
          show-word-limit
        ></el-input>
      </el-form-item>
      <el-form-item label="焦耳/g" prop="heat">
        <el-input
          v-model="form.heat"
          maxlength="10"
          show-word-limit
        ></el-input>
      </el-form-item>
      <el-form-item label="摄入来源" prop="origin_food">
        <addList v-if="id" v-model="form.origin_food" query-type="ingredient" :show-by-id="id"></addList>
      </el-form-item>
      <el-form-item label="摄入选择" prop="origin_select">
        <el-input
          v-model="form.origin_select"
          type="textarea"
          :rows="8"
          maxlength="255"
          show-word-limit
        ></el-input>
      </el-form-item>
    </el-form>
    <div class="operation">
      <div class="cancel_btn" @click="cancel">取消</div>
      <div class="submit_btn" @click="submit">提交</div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
const router = useRouter()
import { ElMessage } from 'element-plus'
import {
  getElementById,
  addElement,
  updateElementById
} from '@/views/settings/backend/api'
import addList from '@/components/addList/index.vue'

let id = ref('')
const initForm = reactive({
  name: '',
  chemical_tag: '',
  main_text: '',
  alias: '',
  image: '',
  icon: '',
  link: '',
  compute: '',
  origin_food: '',
  origin_select: '',
  heat: '', // 热量
  back_color: ''
})
const formRef = ref(null)
const form = ref({ ...initForm })
const rules = reactive({
  name: [
    { required: true, message: '请输入名称', trigger: 'blur' }
  ],
  main_text: [
    { required: true, message: '请输入描述', trigger: 'blur' }
  ],
  heat: [
    { required: true, message: '请输入焦耳/g', trigger: 'blur' }
  ]
})

onMounted(() => {
  const route = useRoute()
  id.value = route.query.id
  getInfo()
})

const getInfo = () => {
  if (!id.value) return
  getElementById(id.value).then(res => {
    form.value = res.result[0]
  })
}

const resetFields = (formData) => {
  const data = formData || initForm
  form.value = { ...data }
  formRef.value.clearValidate()
}

const submit = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      if (id.value) {
        const params = { ...form.value, id: id.value }
        updateElementById(params).then(() => {
          ElMessage({
            type: 'success',
            message: '修改成功'
          })
          cancel()
        })
      } else {
        addElement(form.value).then(() => {
          ElMessage({
            type: 'success',
            message: '添加成功'
          })
          resetFields()
        })
      }
    }
  })
}

const cancel = () => {
  // history.back()
  router.push({ path: '/settings/backend' })
}

</script>

<style lang="scss" scoped>
.element_manager {
  padding: 20px;
  width: calc(100% - 50px);

  .element_title {
    font-size: 20px;
    font-weight: bold;
    margin-bottom: 50px;
    border-left: 5px solid var(--el-color-primary);
    text-align: left;
    padding-left: 10px;
  }

  ::v-deep .el-form-item__label {
    display: block;
    font-weight: bold;
    text-align: left;
  }

  ::v-deep .el-form-item {
    display: block;
  }

  .operation {
    height: 50px;
    display: flex;
    justify-content: end;

    .cancel_btn {
      width: 100px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      border: 1px solid var(--el-color-primary);
      border-radius: 5px;
      margin-right: 20px;
      cursor: pointer;
    }
    .submit_btn {
      width: 100px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      color: #fff;
      background-color: var(--el-color-primary);
      border-radius: 5px;
      cursor: pointer;
    }
  }
}
</style>